import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { NzModalService } from 'ng-zorro-antd';

declare var ui: any;
declare var form: any;
declare var base: any;
declare var security: any;

@Component({
  templateUrl: './f.luggage.different.info.component.html'
})
export class FLuggageDifferentInfoComponent implements OnInit {

  ss: any;
  // 当前数据
  fs: any;
  // 菜单
  menu = null;
  // 字典
  cms_sjryDic: any = null;
  cms_aqjcsbDic: any = null;
  cms_tdyxzkDic: any = null;
  zd_airport: any = null;
  child: any = {};
  index = -1;

  constructor(
    public route: ActivatedRoute,
    private modalService: NzModalService) {
    this.ss = security.SecurityService;
    this.ss.show = false;
    this.menu = new ui.TabService('bpm', 's');
    this.menu.setTab('main');
    // 注册表单
    this.fs = new form.FormSimpleService('luggagedifferent', {
      orderObject: { id: 0 },
      keyField: 'id',
      randomFields: ['id']
    });

    this.cms_sjryDic = new form.DictionaryService('cms_sjry');
    this.cms_aqjcsbDic = new form.DictionaryService('cms_aqjcsb');
    this.cms_tdyxzkDic = new form.DictionaryService('cms_tdyxzk');
    this.zd_airport = new form.DictionaryService('zd_airport');
    this.zd_airport.loadAll();
  }

  ngOnInit() {
    this.initData();

    // 浏览器关闭监听
    window.onbeforeunload = () => {
      // tslint:disable-next-line: deprecation
      const e: any = window.event;
      e.returnValue = ('请确认已保存，是否离开？');
      return e;
    };
  }

  initData() {
    let id = '-1';
    this.route.queryParams.subscribe((res) => {
      if (res.id) {
        id = res.id;
      }
    });
    this.fs.editRowByKey(id, (item) => {
      if (this.fs.er.airport === undefined) {
        this.fs.er.airport =  this.ss.user.userDepCode[0];
      }
      this.fs.closeEdit();
    });
  }

  save() {
    const er = this.fs.er;
    if (!er.id) {
      er.id = base.BaseService.getGUID();
    }
    if (!er.airport || er.airport === ''
      || !er.recorddate || er.recorddate === ''
      || !er.starttime || er.starttime === ''
      || !er.endtime || er.endtime === ''
    ) {
      this.modalService.success({
        nzTitle: '系统提示',
        nzContent: '请填写必填项!'
      });
      return;
    }
    this.fs.save(this.fs.er, (data) => {
      this.modalService.success({
        nzTitle: '系统提示',
        nzContent: '保存成功！'
      });
      this.fs.er = data;
    }, (error) => {
      this.modalService.error({
        nzTitle: '系统提示',
        nzContent: '保存失败！'
      });
    });
  }

  close() {
    window.close();
  }

  // 子表详情编辑
  editRow(row?: any, i?: number) {
    if (row == null) {
      return false;
    }

    this.fs.openEdit();
    this.child = base.BaseService.deepCopy(row);
    this.index = i;
  }

  // 添加数据源子表记录
  addRow() {
    this.fs.openEdit();
    this.child = { id: base.BaseService.getGUID() };
    this.index = -1;
  }

  // 数据源子表弹窗关闭
  childClose() {
    this.fs.closeEdit();
  }

  // 数据源子表弹窗保存
  childSave() {
    if (this.fs.er.record === undefined) {
      this.fs.er.record = [];
    }
    if (this.index === -1) {
      this.fs.er.record.push(this.child);
    } else {
      this.fs.er.record[this.index] = this.child;
    }
    this.fs.closeEdit();
  }
}
